package chen.jia.xin.finalproject.ReadContactors;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import chen.jia.xin.finalproject.R;

public class ContactsActivity extends AppCompatActivity {

    private ListView listView;
    //显示联系人的ListView的适配器
    private AllContactorAdapter adapter;
    //所有的联系人列表
    private List<EachContactor> allContacts = new ArrayList<>();
    //当前要拨打的用户Number
    private String number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contacts);
        listView = findViewById(R.id.phone_contacts);
        adapter = new AllContactorAdapter(this, R.layout.contact_item, allContacts);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                EachContactor eachContactor = allContacts.get(position);
                number = eachContactor.getUser_number();
                if(ContextCompat.checkSelfPermission(ContactsActivity.this,Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
                    //询问权限
                    ActivityCompat.requestPermissions(ContactsActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 2);
                }else {
                    make_phone(number);
                }
            }
        });
        //询问运行时权限(读取手机联系人)
        //先判断是否拥有权限
        if(ContextCompat.checkSelfPermission(this,Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){
            //询问权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 1);
        }else {
            readContacts();
        }
    }

    public void readContacts(){
        Cursor cursor = null;
        try {
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            if (cursor != null) {
                EachContactor eachContactor = null;
                while (cursor.moveToNext()) {
                    String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String displayPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    eachContactor = new EachContactor(displayName,displayPhone);
                    allContacts.add(eachContactor);
                }
                //数据刷新
                adapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            //读取联系人权限
            case 1 :
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    readContacts();
                } else {
                    Toast.makeText(this,"你拒绝了该服务",Toast.LENGTH_SHORT).show();
                }
                break;
            //拨打电话权限
            case 2 :
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    make_phone(number);
                } else {
                    Toast.makeText(this,"你拒绝了该服务",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    //拨打电话
    public void make_phone(String number){
        try {
            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:"+number));
            startActivity(intent);
        } catch (Exception e){
            e.printStackTrace();
        }

    }
}
